﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;

namespace SimdTest2.Methods
{
    [Algorithm(true, AlgorithmType.One)]
    public unsafe class OneMethod : AlgorithmBase
    {
        public OneMethod(int row, int col) : base(row, col)
        {
        }
        protected int[] _data;
        protected override void CreateData()
        {
            _data = new int[_size];
            Random random = new Random();
            for (int i = 0; i < _size; i++)
            {
                _data[i] = random.Next(999);
            }
        }
        protected override void OrdinaryMethod()
        {
            long sum = 0L;
            for (int i = 0; i < _size; i++)
            {
                sum += _data[i];
            }
        }

        protected override void SimdMethod()
        {
            int vectorSize = Vector128<int>.Count;
            int sum4 = 0;
            Vector128<int> sumV = Vector128<int>.Zero;
            fixed (int* p = _data)
            {
                for (int j = 0; j <= _size; j += vectorSize)
                {
                    sumV = Sse2.Add(sumV, *(Vector128<int>*)(p + j));
                }
                int* ptr = (int*)&sumV;
                for (int i = 0; i < vectorSize; i++)
                {
                    sum4 += *(ptr + i);
                }
            }
        }
     
        public byte BinaryStringToByte(string binaryString)
        {
            return Convert.ToByte(binaryString, 2);
        }
        public  int DecimalToBinary(int decimalNumber)
        {
            if (decimalNumber == 0) return 0;

            StringBuilder binary = new StringBuilder();
            while (decimalNumber > 0)
            {
                binary.Insert(0, (decimalNumber % 2)); 
                decimalNumber /= 2; // 右移一位
            }
            return int.Parse(binary.ToString());
        }
    }
}
